<?php

namespace App\Http\Controllers\baseinfo;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\dept;

class DeptController extends Controller
{
    protected $name = '部门档案';
    protected $field = ['deptid', 'deptnm', 'treeid', 'parentid', 'leader', 'leader1', 'leader2', 'isproddept', 'des', 'des1', 'des2'];
    /**
     * 保存
     */
    public function save(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            $isnew = false;
            if (empty($id)) {
                $isnew = true;
            }
            $list = [];
            if (empty($param)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存参数为空']);
            }
            foreach ($param as $key => $value) {
                if (in_array($key, $this->field)) {
                    $list[$key] = $value;
                }
            }
            if (empty($list)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|没有需要处理的数据']);
            }
            if ($isnew) {
                $type = '新增';
                if (empty($list['deptid'])) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => '新增失败!部门ID不能为空']);
                }
                if (empty($list['deptnm'])) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => '新增失败!部门名称不能为空']);
                }
                $FSQL = "  select * from dept where isdel=0 and deptid='" . $list['deptid'] . "'";
                if (!empty(DB::select($FSQL))) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '部门ID已经存在,不可重复']);
                }
                if (!empty($list['parentid'])) {
                    //如果不是根节点
                    $FSQL = "  select * from dept where isdel=0 and treeid='" . $list['parentid'] . "'";
                    if (empty(DB::select($FSQL))) {
                        return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '父节点不存在,不能保存']);
                    }
                    $FSQL = " select MAX(treeid) as maxtree from dept where isdel=0 and parentid=" . intval($list['parentid']);
                    $treeid = DB::select($FSQL)[0]->maxtree + 1;
                    if ($treeid == 1) {
                        $treeid = $list['parentid'] . '01';
                    }
                } else {
                    $FSQL = " select MAX(treeid) as maxtree from dept where isdel=0 and parentid is null";
                    $treeid = DB::select($FSQL)[0]->maxtree + 1;
                }

                $list['treeid'] = $treeid;
                //新增
                $res = dept::create($list);
                if (empty($res)) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '新增失败!']);
                }
            } else {
                $type = '修改';
                $uptab = dept::find($id); // 找到 ID 为 1 的记录
                $uptab->update($list);
                $res = $uptab;
            }
            $FSQL = "SELECT id,deptid,deptnm,treeid,parentid,leader,leader1,leader2,isproddept,des,tabnm,modifier
        FROM dept where isdel=0";
            $data = DB::select($FSQL);
            //记录日志
            $this->log(['type' => $type, 'billcaption' => '部门档案', 'des' => '操作人[' . $type . ']了部门[' . $list['deptnm'] . '],数据为[' . json_encode($list) . ']']);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '保存成功!', 'data' => $data, 'nowdata' => $res]);

        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '保存失败：' . $e->getMessage()]);
        }
    }

    /**
     * 查询
     */
    public function find(Request $request)
    {
        try {
            //$posts = Post::where('title', 'like', '%Post%')->get();
            $param = json_decode($request->param, true);
            $deptid = $this->I($param['deptid'] ?? null);
            $deptnm = $this->I($param['deptnm'] ?? null);
            $where = ' isdel=0';
            if (!empty($deptnm)) {
                $where .= ' and deptnm like "%' . $deptnm . '%"';
            }
            if (!empty($deptid)) {
                $where .= "and deptid = '$deptid'";
            }
            $FSQL = "SELECT COUNT(1) as allcount FROM dept a where  $where";
            $allcount = DB::select($FSQL)[0]->allcount;

            $FSQL = "SELECT id,deptid,deptnm,treeid,parentid,leader,leader1,leader2,isproddept,des,tabnm,modifier
              FROM dept where   $where";
            $data = DB::select($FSQL);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'count' => $allcount, 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            if (empty($id)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!部门ID为空']);
            }

            $uptab = dept::find($id); // 找到 ID 为 1 的记录
            if (empty($uptab)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!部门不存在']);
            }
            $uptab->isdel = 1;
            $res = $uptab->save();
            if (empty($res)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!']);
            }
            $FSQL = "SELECT id,deptid,deptnm,treeid,parentid,leader,leader1,leader2,isproddept,des,tabnm,modifier
        FROM dept where isdel=0";
            $data = DB::select($FSQL);
            //记录日志
            $this->log(['type' => '删除', 'billcaption' => '部门档案', 'des' => '操作人[删除]了部门[' . $uptab->deptnm . '],ID为[' . $uptab->deptid . ']']);
            return $this->ajaxReturn(['code' => 1, 'msg' => '删除成功!', 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|删除失败!','error'=>$e->getMessage()]);
        }
    }
}
